<%--
  - Author		:	Jaewon Choi
  --%>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/pages/includes/taglibs.jspf" %>
<%@ include file="/WEB-INF/pages/includes/variables.jspf" %>
<%@ include file="/WEB-INF/pages/includes/doctype.jspf" %>
<c:choose>
	<c:when test="${not empty advertising}">
		<c:set var="fAct" value="수정" />
	</c:when>
	<c:otherwise>
		<c:set var="fAct" value="등록" />
	</c:otherwise>
</c:choose>
<html>
	<head>
<%@ include file="/WEB-INF/pages/includes/metatags.jspf" %>
		<title><fmt:message key="pages.title" /></title>
		<link rel="stylesheet" type="text/css" href="<c:url value="${dirStyles}/common.css" />" />

		<link rel="stylesheet" type="text/css" href="<c:url value="${dirScripts}/jqueryUi/css/ui-lightness/jquery-ui-1.8.21.custom.css" />" />

		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-1.7.2.min.js" />"></script>
		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-ui-1.8.21.custom.min.js" />"></script>
		<script type="text/javascript">
			$(function(){
				$("#name").focus();
				
				$( ".date" ).datepicker({
					showOn: "both",
					buttonImage: "<c:url value="${dirImages}/calendar.png" />",
					buttonImageOnly: true,
					dateFormat: "yy-mm-dd"
				});
			});
			
			function chkDuplNm(){
				var idx = "0";
				var name = document.frm.name.value;
				if(document.frm.idx){
					idx = document.frm.idx.value;
				}
				
				$.ajax({
					type: "POST",
					url: "<c:url value="/ad/chkDupl" />",
					dataType: "json",
					async: false,
					data: {
								"idx" : idx,
								"chkName" : name
							},
					success: function(data){
						if(data.resultCode == "200"){
							chkDuplNmFlag = data.result;
							if(! eval(chkDuplNmFlag)){
								$(document.frm.name).css("background", "#ADC900");
							}else{
								$(document.frm.name).css("background", "#F2685E");
							}
						}else{
							alert(data.errMsg);
						}
					},
					beforeSend: function(){
					},
					error: function(jqXHR, textStatus, errorThrown){
						//alert(jqXHR.responseText); //에러 페이지 내용
						alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						throw errorThrown;
					},
					complete: function(){
					}
				 });
				return eval(chkDuplNmFlag);
			}

			function validation(){
				try{
					/*
					if($(".categoryIdx option:selected").val().replace(/\s/g, '') == "") {
						alert("[브랜드]은 필수 선택 항목입니다.");
						document.frm.categoryIdx.focus();
						return false;
					}
					*/
					if((document.frm.name.value).replace(/\s/g, '') == ""){
						alert("[이슈명]은 필수 입력 항목입니다.");
						document.frm.name.focus();
						return false;
					}
					/*
					if(chkDuplNm()){
						alert("[광고명]은 중복될 수 없습니다.");
						document.frm.name.focus();
						return false;
					}
					*/
					if($(".dateFlag:checked").length == 0){
						if((document.frm.beginTime.value).replace(/\s/g, '') == ""){
							alert("[이슈 시작일]은 필수 입력 항목입니다.");
							document.frm.beginTime.focus();
							return false;
						}
						if((document.frm.endTime.value).replace(/\s/g, '') == ""){
							alert("[이슈 종료일]은 필수 입력 항목입니다.");
							document.frm.endTime.focus();
							return false;
						}
					}

					<c:if test="${empty advertising}">
						if(($("#adImgFile").val()).replace(/\s/g, '') == ""){
							alert("[배너이미지]는 필수 입력 항목입니다.");
							document.frm.coverCurl.focus();
							return false;
						}
					</c:if>
					
					if($(".actionType:checked").val() != "N"){
						document.frm.adFileDatas.value=$(".actionType:checked").val()+"^";
						if($(".actionType:checked").val() == "CO"){
							//document.frm.adFileDatas.value+=document.frm.contIdx.value;	
							if((document.frm.conIdx.value).replace(/\s/g, '') == ""){
								alert("[컨텐츠IDX]는 필수 입력 항목입니다.");
								document.frm.conIdx.focus();
								return false;
							}
							document.frm.adFileDatas.value+=document.frm.conIdx.value;
						}else if($(".actionType:checked").val() == "E"){
							document.frm.adFileDatas.value+=document.frm.eventIdx.value;	
						}else{
							if((document.frm.link.value).replace(/\s/g, '') == ""){
								alert("[링크 URL]는 필수 입력 항목입니다.");
								document.frm.link.focus();
								return false;
							}
							document.frm.adFileDatas.value+=document.frm.link.value;
						}
					}else{
						document.frm.adFileDatas.value="N";
					}
				}catch(err){
					return false;	
				}
				
				return confirm("${fAct} 하시겠습니까?");
			}
		</script>
	</head>
	<body>
		<div class="bodyWrap">
<%@ include file="/WEB-INF/pages/includes/header.jsp" %>
<%@ include file="/WEB-INF/pages/includes/left.jsp" %>
			<div class="contWrap">
				<div class="menuTitle">
					이슈${fAct}
				</div>
				<form id="frm" name="frm" method="post" enctype="multipart/form-data" action="<c:url value="/ad/submit.do" />" onsubmit="return validation();">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
					<input type="hidden" name="searchAdCategoryId" value="${param.searchAdCategoryId}" />
					<input type="hidden" name="adCategoryIdx" value="1" /> 
					<table class="frmTbl">
						<colgroup>
							<col width="176px;" />
							<col width="100px;" />
							<col width="*" />
						</colgroup>
						<tbody>
			 				<c:if test="${not empty advertising}">
			 					<tr>
									<th>이슈 IDX</th>
									<td colspan="2">
										${advertising.idx}
										<input type="hidden" name="idx" value="${advertising.idx}">
									</td>
								</tr>
			 				</c:if>			 				
							<tr>
								<th>이슈명 *</th>
								<td colspan="2">
									<input type="text" id="name" name="name" value="${advertising.name}" style="width: 600px;" maxlength="15" title="광고명 입니다." />
								</td>
							</tr>
							<tr>
								<th>기간 *</th>
								<td colspan="2">
									<label>
										<input type="checkbox" class="dateFlag" <c:if test="${empty advertising.beginTime && empty advertising.endTime}"> checked="checked" </c:if> onclick="javascript:if(this.checked){$('.date').attr('disabled','disabled');}else{$('.date').removeAttr('disabled');};"> 항상
									</label>
									<c:catch var="e">
										<fmt:parseDate var="beginTime" pattern="yyyy-MM-dd HH:mm:ss" value="${advertising.beginTime}" />
										<fmt:formatDate var="beginTime" value="${beginTime}" pattern="yyyy-MM-dd" />
									</c:catch>
									<c:if test="${not empty e}">
										<c:set var="beginTime" value="${advertising.beginTime}" />
									</c:if>
									<c:catch var="e">
										<fmt:parseDate var="endTime" pattern="yyyy-MM-dd HH:mm:ss" value="${advertising.endTime}" />
										<fmt:formatDate var="endTime" value="${endTime}" pattern="yyyy-MM-dd" />
									</c:catch>
									<c:if test="${not empty e}">
										<c:set var="endTime" value="${advertising.endTime}" />
									</c:if>
									<input type="text" class="date" name="beginTime" <c:if test="${empty advertising.beginTime && empty advertising.endTime}"> disabled="disabled" </c:if> readonly="readonly" value="${beginTime}" />
									~
									<input type="text" class="date" name="endTime" <c:if test="${empty advertising.beginTime && empty advertising.endTime}"> disabled="disabled" </c:if> readonly="readonly" value="${endTime}" />
								</td>
							</tr>
							<c:forEach items="${adFileList}" var="adFile">
								<c:if test="${adFile.fileType eq 'adImg'}">
									<c:set var="adFileIdx" value="${adFile.idx}" />
									<c:set var="adImgUrl" value="${adFile.fileUrl}" />
									<c:set var="actionType" value="${fn:split(adFile.fileData, '^')[0]}" />
									<c:set var="actionVal" value="${fn:split(adFile.fileData, '^')[1]}" />
								</c:if>
							</c:forEach>
							<tr>
								<th>배너 이미지 *</th>
								<td colspan="2">
									<input type="file" id="adImgFile" name="adFiles[0]" />
									<c:if test="${not empty adImgUrl}">
										<br />
										<img src="<c:out value="${adImgUrl}" />" class="thumbnailImg" />
									</c:if>
								</td>
							</tr>
							<tr>
								<th rowspan="2">이슈 링크 *</th>
								<td colspan="2">
									<label>
										<input type="radio" name="actionType" class="actionType" value="CO" <c:if test="${empty advertising || actionType eq 'CO'}"> checked="checked" </c:if> onclick="javascript:$('.fileDataTr').hide();$('.'+this.value).show();"> 컨텐츠
									</label>	
									<label>
										<input type="radio" name="actionType" class="actionType" value="IL" <c:if test="${actionType eq 'IL'}"> checked="checked" </c:if> onclick="javascript:$('.fileDataTr').hide();$('.'+this.value).show();"> URL
									</label>			
									<label>
										<input type="radio" name="actionType" class="actionType" value="N" <c:if test="${actionType eq 'N'}"> checked="checked" </c:if> onclick="javascript:$('.fileDataTr').hide();$('.'+this.value).show();"> 이동하지 않음
									</label>
									<input type="hidden" name="adFileIdx" value="${adFileIdx}" />
									<input type="hidden" name="adFileTypes" value="adImg" />
									<input type="hidden" name="adFileDatas" value="" />
								</td>
							</tr>
							<tr class="fileDataTr C" <c:if test="${not empty advertising && (actionType ne 'CO')}"> style="display: none;" </c:if>>
								<td>
									컨텐츠 IDX
								</td>
								<td>
									<input type="text" name="conIdx" style="width: 500px;" <c:if test="${actionType eq 'CO'}">value="${actionVal}"</c:if> />
								</td>
							</tr>
							<tr class="fileDataTr IL OL" <c:if test="${(actionType ne 'IL' && actionType ne 'OL')}"> style="display: none;" </c:if>>
								<td>
									URL
								</td>
								<td>
									<input type="text" name="link" style="width: 500px;" <c:if test="${actionType eq 'IL' || actionType eq 'OL'}">value="${actionVal}"</c:if> />
								</td>
							</tr>
							<tr class="fileDataTr N" <c:if test="${actionType ne 'N'}"> style="display: none;" </c:if>>
								<td>
									이동하지 않음
								</td>
								<td>
								</td>
							</tr>	
							 <input type="hidden" name="urlType" class="urlType" value="01">
						</tbody>
					</table>
					<input type="button" class="btn" value="취소" onclick="javascript:document.listFrm.submit();" />
					<input type="submit" class="btn" value="${fAct}" />
					<c:if test="${not empty advertising}">
						<input type="button" class="btn" value="삭제" onclick="javascript:if(confirm('삭제 하시겠습니까?')){document.removeFrm.submit();}" />
					</c:if>
				</form>
				<form name="listFrm" action="<c:url value="/ad/list.do" />" method="post" style="display: none;">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="name" value="${param.searchName}" />
					<input type="hidden" name="adCategoryIdx" value="${param.searchAdCategoryIdx}" />
				</form>
				
				<form name="removeFrm" action="<c:url value="/ad/remove.do" />" method="post" style="display: none;">
					<input type="hidden" name="fAct" value="remove">
					<input type="hidden" name="idx" value="${advertising.idx}">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
					<input type="hidden" name="searchAdCategoryIdx" value="${param.searchAdCategoryIdx}" />
				</form>
			</div>
<%@ include file="/WEB-INF/pages/includes/footer.jsp" %>
		</div>
	</body>
</html> 